home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 43 / Mac Magazin and MacEasy Magazine CD - Issue 43.iso / Software / Entwickler / CCMArea / Source / CCMArea.cp < prev    next >
Encoding:
Text File  |  1998-02-12  |  6.2 KB  |  240 lines  |  [TEXT/CWIE]

  1. // CCMArea version 1.3.1
  2. // 11-11-97
  3. // by David Catmull
  4.  
  5. /* History:
  6.     
  7.     9-19-97        First version
  8.     
  9.     10-6-97        Added optional references to UCursor
  10.                         Added sCursorID and SetCursorID
  11.                         Deletes trailing separators
  12.                         Added more comments
  13.     
  14.     10-16-97    Modified GetSelectionDesc to call mSuperCMArea
  15.                         Made CMAdjustCursor static
  16.     
  17.     10-18-97    Call ProcessCommand instead of ObeyCommand
  18.     
  19.     10-21-97    Moved AddCommandToMenu, SetCMCursor, SetCursorID and intialization to UCMArea
  20.                         Removed CMAdjustCursor
  21.     
  22.     11-11-97    Added delay click
  23.     
  24.     11-19-97    Pass mouseDown to DelayClick
  25.     
  26.     11-22-97    Multiple return values for CMClick
  27.     
  28.     11-29-97    Added PostClick
  29.     
  30.     12-20-97    Removed BalloonAvailable
  31.     
  32.     2-12-98        Disposes of menus before handling selection
  33. */
  34.  
  35. #include "CCMArea.h"
  36. #include "UCMArea.h"
  37. #include "CCMWindow.h"
  38. #include <LCommander.h>
  39. #include <LMenuBar.h>
  40. #include <LMenu.h>
  41.  
  42. #include <Appearance.h>
  43.  
  44. Int16 CCMArea::sMenuID = 999;
  45.  
  46.  
  47. /* ------------------------------------------------------------------------------------------------ */
  48.  
  49. CCMArea::CCMArea(CCMArea *inSuperCMArea)
  50. {
  51.     mSuperCMArea = inSuperCMArea;
  52. }
  53.  
  54. /* ------------------------------------------------------------------------------------------------ */
  55. // CMClick
  56. //    Build the menu, display it, and pass on the selected command
  57.  
  58. short
  59. CCMArea::CMClick(const SMouseDownEvent &inMouseDown)
  60. {
  61.     if (!UCMArea::HasContextualMenus())
  62.         return cm_Nothing;
  63.     
  64.     if (!::IsShowContextualMenuClick(&inMouseDown.macEvent)) {
  65.         if (DelayClick(inMouseDown)) {
  66.             if (!UCMArea::WaitDelayClick(inMouseDown.macEvent))
  67.                 return cm_Nothing;
  68.         }
  69.         else
  70.             return cm_Nothing;
  71.     }
  72.     
  73.     MenuHandle menu = ::NewMenu(sMenuID,"\p");
  74.     AEDesc selection = { typeNull,0L };
  75.     Str255 helpString,itemText;
  76.     UInt32 userSelection,*commandList;
  77.     SInt16 menuID;
  78.     UInt16 menuItem;
  79.     OSStatus err;
  80.     short i,itemCount;
  81.     short returnValue = cm_Nothing;
  82.     unsigned long selectedCommand = cmd_Nothing;
  83.     
  84.     PreClick(inMouseDown);
  85.     BuildMenu(menu);
  86.     GetSelectionDesc(selection);
  87.     
  88.     // Check for a trailing divider and remove it
  89.     // Other wise we end up with two dividers if the plugins add anything
  90.     
  91.     ::GetMenuItemText(menu,::CountMenuItems(menu),itemText);
  92.     if (itemText[1] == '-')
  93.         ::DeleteMenuItem(menu,::CountMenuItems(menu));
  94.     
  95.     // Somehow ContextualMenuSelect strips out all the command IDs from the main menu,
  96.     // so we have to make a backup list. Fortunately, submenus are unaffected.
  97.     
  98.     itemCount = ::CountMenuItems(menu);
  99.     
  100.     StPointerBlock commandBlock(sizeof(UInt32) * itemCount);
  101.     
  102.     commandList = (UInt32*) (Ptr)commandBlock;
  103.     for (i = 1; i <= itemCount; i++)
  104.         ::GetMenuItemCommandID(menu,i,&commandList[i-1]);
  105.     
  106.     ::InsertMenu(menu,hierMenu);
  107.     GetHelpItemString(helpString);
  108.     err = ::ContextualMenuSelect(menu,inMouseDown.macEvent.where,
  109.                                                                 false,HelpType(),helpString,
  110.                                                                 (selection.descriptorType == typeNull) ? 0L : &selection,
  111.                                                                 &userSelection,&menuID,&menuItem);
  112.     
  113.     returnValue = cm_MenuDisplayed;
  114.     
  115.     CleanUpMenus();
  116.     ::DeleteMenu(sMenuID);
  117.     ::DisposeMenu(menu);
  118.     
  119.     switch (userSelection) {
  120.         
  121.         case kCMMenuItemSelected:
  122.             if (menuID == sMenuID)
  123.                 selectedCommand = commandList[menuItem-1];
  124.             else {
  125.                 MenuHandle subMenu;
  126.                 
  127.                 subMenu = ::GetMenuHandle(menuID);
  128.                 ::GetMenuItemCommandID(subMenu,menuItem,&selectedCommand);
  129.             }
  130.             LCommander::GetTarget()->ProcessCommand(selectedCommand);
  131.             returnValue = cm_ItemChosen;
  132.             break;
  133.         
  134.         case kCMShowHelpSelected:
  135.             ShowHelp();
  136.             returnValue = cm_ItemChosen;
  137.             break;
  138.         
  139.         case kCMNothingSelected:
  140.         case kCMShowBalloonSelected:    // Balloons are handled automatically
  141.         default:
  142.             break;
  143.     }
  144.     
  145.     PostClick(inMouseDown,returnValue,selectedCommand);
  146.     
  147.     return returnValue;
  148. }
  149.  
  150. /* ------------------------------------------------------------------------------------------------ */
  151. // GetSelectionDesc
  152. //    Return an Apple Event descriptor for the selection data
  153. //    Override to make your data available to CM plugins
  154.  
  155. void
  156. CCMArea::GetSelectionDesc(AEDesc &outSelDesc)
  157. {
  158.     if (mSuperCMArea)
  159.         mSuperCMArea->GetSelectionDesc(outSelDesc);
  160. }
  161.  
  162. /* ------------------------------------------------------------------------------------------------ */
  163. // PreClick
  164. //    Do any processing necessary before displaying the contextual menu
  165. //    such as updating the command chain - LCommander::SwitchTarget(this)
  166.  
  167. void
  168. CCMArea::PreClick(const SMouseDownEvent &inMouseDown)
  169. {
  170. #pragma unused (inMouseDown)
  171. }
  172.  
  173. /* ------------------------------------------------------------------------------------------------ */
  174. // PostClick
  175. //    Do any post-processing necessary after displaying the contextual menu
  176. //    inCMResult is the same value to be returned by CMClick
  177. //        but it will never be cm_Nothing
  178. //    inCommand is the command that was selected, if any (cmd_Nothing otherwise)
  179.  
  180. void
  181. CCMArea::PostClick(const SMouseDownEvent &inMouseDown,short inCMResult,CommandT inCommand)
  182. {
  183. #pragma unused (inMouseDown)
  184. #pragma unused (inCMResult)
  185. #pragma unused (inCommand)
  186. }
  187.  
  188. /* ------------------------------------------------------------------------------------------------ */
  189. // BuildMenu
  190. //    Construct the menu to be displayed
  191. //    If the superCMArea added any items, append a separator
  192.  
  193. void
  194. CCMArea::BuildMenu(MenuHandle inMenu)
  195. {
  196.     short itemCount = ::CountMenuItems(inMenu);
  197.     
  198.     if (mSuperCMArea && AddSuperCommands())
  199.         mSuperCMArea->BuildMenu(inMenu);
  200.     
  201.     // Add a separator if the superCMArea added any commands
  202.     if (::CountMenuItems(inMenu) != itemCount)
  203.         ::AppendMenu(inMenu,"\p-");
  204.     
  205.     BuildMenuSelf(inMenu);
  206. }
  207.  
  208. /* ------------------------------------------------------------------------------------------------ */
  209. // BuildMenuSelf
  210. //    Override to add your own commands
  211.  
  212. void
  213. CCMArea::BuildMenuSelf(MenuHandle inMenu)
  214. {
  215. #pragma unused (inMenu)
  216. }
  217.  
  218. /* ------------------------------------------------------------------------------------------------ */
  219. // DelayClick
  220. //    Determine whether to use delay click
  221.  
  222. Boolean
  223. CCMArea::DelayClick(const SMouseDownEvent &inMouseDown)
  224. {
  225. #pragma unused (inMouseDown)
  226.     
  227.     return UCMArea::GetDelayClick();
  228. }
  229.  
  230. /* ------------------------------------------------------------------------------------------------ */
  231. // ShowHelp
  232. //    Called when the user selects Help from the menu
  233. //    You can open your Apple Guide file or whatever other help system you use
  234.  
  235. void
  236. CCMArea::ShowHelp()
  237. {
  238. }
  239.  
  240.